//
//  LeftViewController.m
//  唧歪
//
//  Created by ZC on 15/7/7.
//  Copyright (c) 2015年 MWJ. All rights reserved.
//

#import "LeftViewController.h"
#import "AppDelegate.h"
#import "PersonalViewController.h"  //个人信息界面
#import "FriendViewController.h"  //好友查看界面
#import "MyPostViewController.h"  //我的帖子界面
#import "SetUpViewController.h"  //设置界面
#import "RegisteredViewController.h" //注册登录
#import "myFocusViewController.h"  //我关注的
#import "FocusMeViewController.h"  //关注我的

#define personalDataURL @"https://app.jiwaipro.com:8888/index.php/JiWai/PersonalCenter" //个人资料URL

@interface LeftViewController (){
    AppDelegate *app;
    UIAlertView *_alert; //清除缓存对话框
    NSMutableArray *Personalarray;
}

@end

@implementation LeftViewController
#pragma mark - 页面即将加载
- (void)viewWillAppear:(BOOL)animated
{
    [app isConnectionAvailable];
    if ([app.Existence isEqualToString:@"1"]) {
    }else{
        [super viewWillAppear:animated];
        AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [tempAppDelegate.LeftSlideVC setPanEnabled:YES];
        if ((app.user_id == nil)) {
            _imageview.image = [UIImage imageNamed:@"LOGO80"];
        }else{
        [self loading];
        }
        app.Existence = @"0";
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    app=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    app.defaults = [NSUserDefaults standardUserDefaults];
    app.user_id = [app.defaults objectForKey:@"yonghu"];
    //tableview顶端不出现空白
    self.automaticallyAdjustsScrollViewInsets=NO;
    UIImageView *bjimage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen ].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    bjimage.image = [UIImage imageNamed:@"wu.jpg"];
    UIView *leftview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width/3, [UIScreen mainScreen].bounds.size.height)];
    [self.view addSubview:bjimage];
    [self.view addSubview:leftview];
    //头像
    _imageview = [[UIImageView alloc] initWithFrame:(CGRectMake((leftview.bounds.size.width-80)/2, 100, 80, 80))];
    _imageview.layer.masksToBounds = YES;
    _imageview.layer.cornerRadius = _imageview.bounds.size.width/2;
    [leftview addSubview:_imageview];
    //tableView
    UITableView *tabview = [[UITableView alloc] initWithFrame:CGRectMake(0, 200, leftview.bounds.size.width, 280)];
    tabview.dataSource = self;
    tabview.delegate = self;
    tabview.scrollEnabled = NO;
    CGFloat R  = (CGFloat) 255/255.0;
    CGFloat G = (CGFloat) 255/255.0;
    CGFloat B = (CGFloat) 255/255.0;
    CGFloat alpha = (CGFloat) 0.0;
    UIColor *myColorRGB = [ UIColor colorWithRed: R  green: G  blue: B  alpha: alpha  ];
    leftview.backgroundColor = myColorRGB;
    tabview.backgroundColor = myColorRGB;
    [leftview addSubview:tabview];
}
-(void)loading{
    Personalarray = [NSMutableArray array];
    NSString *poststr =[NSString stringWithFormat:@"user_id=%@",app.user_id];
    [self londingPath:personalDataURL Str:poststr];
}
-(void)londingPath:(NSString *)path Str:(NSString *)string{
    NSURL *url = [NSURL URLWithString:path];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        Personalarray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        [self performSelectorOnMainThread:@selector(refreshData:) withObject:Personalarray waitUntilDone:YES];
    }];
}
- (void)refreshData:(NSArray *)sender{
    NSString *headportrait = [NSString stringWithFormat:@"%@",sender[0][0][@"user_headPortrait"]];
    NSURL *url=[NSURL URLWithString:headportrait];
    _imageview.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
}
#pragma mark - 返回行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 7;
}
#pragma mark - 返回cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}
#pragma mark - cell的内容显示
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdentifier=@"cell";
    
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    NSArray *array = [[NSArray alloc] initWithObjects:@"个人信息",@"我的帖子",@"好友查看",@"我关注的",@"关注我的",@"设置",@"清除缓存", nil];
    
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.textLabel.text=array[indexPath.row];
    cell.textLabel.font=[UIFont fontWithName:@"arail" size:10];
    
    CGFloat R  = (CGFloat) 255/255.0;
    CGFloat G = (CGFloat) 255/255.0;
    CGFloat B = (CGFloat) 255/255.0;
    CGFloat alpha = (CGFloat) 0.5;
    UIColor *myColorRGB = [ UIColor colorWithRed: R  green: G  blue: B  alpha: alpha  ];
    cell.backgroundColor = myColorRGB;
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //个人信息
    if (indexPath.row == 0) {
        if (app.user_id == nil||[app.user_id isEqualToString:@""]) {
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
            AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            RegisteredViewController *reg = [sb instantiateViewControllerWithIdentifier:@"RegisteredVC"];
            [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
            [tempAppDelegate.mainNavigationController pushViewController:reg animated:YES];
        }else{
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        PersonalViewController *personal = [sb instantiateViewControllerWithIdentifier:@"personalVC"];
        [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
        [tempAppDelegate.mainNavigationController pushViewController:personal animated:YES];
        }
    }
    //我的帖子
    if (indexPath.row == 1) {
        if (app.user_id == nil||[app.user_id isEqualToString:@""]) {
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
            AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            RegisteredViewController *reg = [sb instantiateViewControllerWithIdentifier:@"RegisteredVC"];
            [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
            [tempAppDelegate.mainNavigationController pushViewController:reg animated:YES];
        }else{
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        MyPostViewController *myPost = [sb instantiateViewControllerWithIdentifier:@"MyPostVC"];
        [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
        [tempAppDelegate.mainNavigationController pushViewController:myPost animated:YES];
        }
    }
    //好友查看
    if (indexPath.row == 2) {
        if (app.user_id == nil||[app.user_id isEqualToString:@""]) {
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
            AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            RegisteredViewController *reg = [sb instantiateViewControllerWithIdentifier:@"RegisteredVC"];
            [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
            [tempAppDelegate.mainNavigationController pushViewController:reg animated:YES];
        }else{
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        FriendViewController *friend = [sb instantiateViewControllerWithIdentifier:@"friendVC"];
        [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
        [tempAppDelegate.mainNavigationController pushViewController:friend animated:YES];
        }
    }
    //我关注的
    if (indexPath.row == 3) {
        if (app.user_id == nil||[app.user_id isEqualToString:@""]) {
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
            AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            RegisteredViewController *reg = [sb instantiateViewControllerWithIdentifier:@"RegisteredVC"];
            [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
            [tempAppDelegate.mainNavigationController pushViewController:reg animated:YES];
        }else{
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
            AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            myFocusViewController *myFocus = [sb instantiateViewControllerWithIdentifier:@"MyFocusVC"];
            [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
            [tempAppDelegate.mainNavigationController pushViewController:myFocus animated:YES];
        }
    }
    //关注我的
    if (indexPath.row == 4) {
        if (app.user_id == nil||[app.user_id isEqualToString:@""]) {
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
            AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            RegisteredViewController *reg = [sb instantiateViewControllerWithIdentifier:@"RegisteredVC"];
            [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
            [tempAppDelegate.mainNavigationController pushViewController:reg animated:YES];
        }else{
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
            AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            FocusMeViewController *focusMe = [sb instantiateViewControllerWithIdentifier:@"FocusMeVC"];
            [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
            [tempAppDelegate.mainNavigationController pushViewController:focusMe animated:YES];
        }
    }
    //设置
    if (indexPath.row == 5) {
        if (app.user_id == nil||[app.user_id isEqualToString:@""]) {
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
            AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            RegisteredViewController *reg = [sb instantiateViewControllerWithIdentifier:@"RegisteredVC"];
            [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
            [tempAppDelegate.mainNavigationController pushViewController:reg animated:YES];
        }else{
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        SetUpViewController *setUp = [sb instantiateViewControllerWithIdentifier:@"setUpVC"];
        [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
        [tempAppDelegate.mainNavigationController pushViewController:setUp animated:YES];
        }
    }
    //清除缓存
    if (indexPath.row == 6) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您是否确定要清除缓存" delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
        _alert=alert;
        [alert show];
    }

}
#pragma mark - “是”按钮执行方法

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==1&&alertView==_alert){
    
    dispatch_async(
                   dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
                   , ^{
                       NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
                       
                       NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
                       for (NSString *p in files) {
                           NSError *error;
                           NSString *path = [cachPath stringByAppendingPathComponent:p];
                           if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                               [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                           }
                       }
                       [self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:nil waitUntilDone:YES];});
    }
}

#pragma mark - 清除缓存方法
-(void)clearCacheSuccess
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"缓存清除成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alert show];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
